var lang ;
$(document).ready(function(){
	lang = $("#display-lang").val();
	$(".nav li").click(function(){
		var old = $(this).siblings(".active");
		$(old).removeClass("active");
		$(this).addClass("active");
		showDiv($(this));
	});
	loadData($("#summaryDiv"));
	loadData($("#entitiesDiv"));
	$("#operateBtn").live("click", function(){
		var text = $(this).val();
		if(text == "Join in"){
			applyForJoin($(this));
		}else if(text == "Leave"){
			leaveGroup($(this));
		}
	});
});

function applyForJoin(operateBtn){
	
}

function showDiv(selectedNav){
	var divName = "#"+$(selectedNav).attr("id").replace("Nav","Div");
//	alert(divName+"|"+$(divName).length);
	$(divName).show();
	$(divName).siblings(".panel").hide();
	var loadName = divName+".origin";
//	alert(loadName+"|"+$(loadName).length);
	if($(loadName).length>0){
		loadData($(loadName));
		$(loadName).removeClass("origin");
	}
}
function loadData(panel){
	$(panel).addClass("loading2");
	var gid = $("#gid").val();
	var reqType = $(panel).attr("id");
	var data = {};
	data["gid"] = gid;
	data["lang"] = lang;
	data = JSON.stringify(data);
	data = (reqType+"="+encodeURIComponent(data));
//	alert("data2:"+data);
	$.ajax({
		url: "/sview/profileServlet",
		data: data,
		dataType: 'json',
		type: 'post',
		success: function(result){
			$(panel).removeClass("loading2");
			var type = result["type"];
			if(type == "string"){
				$(panel).html(result["result"]);
			}else if(type == "table"){
//				alert(result["result"]);
				var table = panel.children("table");
//				alert($(table).html());
				$(table).append(result["result"]);
//				alert($(table).html());
			}else if(type == "error"){
				$(panel).html(result["result"]);
			}
		}
	})
}
function join(){
	var displayLang = $("#display-lang").val();
	var uid=$("#uid").val();
	var data={};
	data["action"] = "join";
	data["value"] = uid;
	data["gid"] = $("#gid").val();
	data = JSON.stringify(data);
	data = ("gcreate="+encodeURIComponent(data));
	$.ajax({
		url:"/sview/gcreateValidate",
		async: false,
		data: data,
		dataType: "text",
		type: "post",
		success: function(text){
			var displayLang = $("#display-lang").val();
			var location;
			var gid=$("#gid").val();
			if(displayLang=="en"){
				location = "/sview/group/gprofile.jsp?gid="+gid;
			}
			else if(displayLang=="zh"){
				location = "/sview/group/gprofile_zh.jsp?gid="+gid; 
			}
			if(text!="null" && text!=null){
				alert(text);
				window.location = location;
			}else{
				if(displayLang == 'en'){
					alert("failed! please try later");
				}else if(displayLang == 'zh'){
					alert("操作失败，请稍后再试");
				}
				
			}
		}
	});
}
function leave(){
	var flag = $("#flag").val();;
	var uname=$("#uname").val();
	var gname=$("#gname").val();
	var displayLang = $("#display-lang").val();
		if(flag == 'member' || flag=='isGroup'){
			var showText;
			if(displayLang == 'en'){
				showText = "Are you sure to leave the edit group?";
			}else{
				showText = "是否确定要离开该编辑组?";
			}
			if(confirm(showText)){
				var data={};
				data["action"]="memLeave";
				data["gname"]= gname;
				data["unames"]=uname;
				data = JSON.stringify(data);
				data = ("msg="+encodeURIComponent(data));
				$.ajax({
					url:'/sview/groupDetailServlet',
					dataType:'text',
					type:'post',
					data:data,
					success:function(text){
						var displayLang = $("#display-lang").val();
						if(displayLang == 'en'){
							alert("deleted!");
						}else if(displayLang == 'zh'){
							alert("已删除!");
						}
						if(flag == 'member')
							window.location.reload();
						else
							logout(displayLang);
					}
				});
			}
		}else if(displayLang == 'en'){
			alert("cannot delete yourself!");
		}else if(displayLang == 'zh'){
			alert("不能删除自己!");
		}
}